package com.cjr.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cjr.pojo.SeckillOrder;
import com.cjr.mapper.SeckillOrderMapper;
import com.cjr.pojo.User;
import com.cjr.service.ISeckillOrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

/**
 * @author cjr
 */
@Service
public class SeckillOrderServiceImpl extends ServiceImpl<SeckillOrderMapper, SeckillOrder> implements ISeckillOrderService {

    @Autowired
    private SeckillOrderMapper seckillOrderMapper;

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;


    @Override
    public Long getResult(User user, Long goodsId) {
        SeckillOrder seckillOrder = seckillOrderMapper.selectOne(
                new QueryWrapper<SeckillOrder>()
                .eq("user_id", user.getId())
                .eq("goods_id", goodsId));
        if (seckillOrder != null) {
            return seckillOrder.getOrderId();
        } else {
            if (Boolean.TRUE.equals(redisTemplate.hasKey("isStockEmpty:" + goodsId))) {
                return -1L;
            }else {
                return 0L;
            }
        }
    }
}
